package com.lizemin.power.controller;

import com.lizemin.power.threadPool.PowerThreadPoolExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author lzm
 * @date 2025/6/5 19:38
 * @description
 */
@Slf4j
@RestController
public class ThreadPoolController {

    /**
     * JDK线程池
     */
    @Autowired
    @Qualifier("orderThreadPoolExecutor")
    private ThreadPoolExecutor threadPoolExecutor;

    /**
     * 自己手写的线程池
     */
    @Autowired
    private PowerThreadPoolExecutor powerThreadPoolExecutor;

    /**
     * 测试使用JDK自带的线程池
     *
     * @return 执行结果
     */
    @GetMapping("/createOrder")
    public String createOrder() {
        threadPoolExecutor.execute(() -> {
            log.info("线程{} => 开始创建订单", Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        return "success";
    }

    /**
     * 测试使用自己手写的线程池
     *
     * @return 执行结果
     */
    @GetMapping("/createOrderWithPowerThreadPool")
    public String createOrderWithPowerThreadPool() {
        powerThreadPoolExecutor.execute(() -> {
            log.info("自定义线程{} => 开始创建订单", Thread.currentThread().getName());
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        return "success";
    }


}
